package com.zsrt.easyui.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.zsrt.easyui.user.entity.UserEntity;
import com.zsrt.easyui.user.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService service;
	
	
	@ResponseBody
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public Map<String,Object>  add(UserEntity entity){
		System.out.println("进入新增方法...");
		Map<String,Object> map = new HashMap<String,Object>();
		service.add(entity, map);
		return map;
		
		
	}
	
	@ResponseBody
	@RequestMapping("/findWithPage")
	public Map<String,Object> findWithPage(Integer page,Integer rows,String userName){
		Integer before = rows*(page -1);
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("before", before);
		param.put("after", rows);
		if(null != userName && !"".equals(userName)){
			param.put("userName", "%"+userName+"%");
		}
		
		Map<String,Object> jieguo = new HashMap<String,Object>();
		service.findWithPage(param,jieguo);
		
		return jieguo;
	}
	
	@RequestMapping(value="/findById")
	@ResponseBody
	public UserEntity findById(Integer id){
		UserEntity entity =service.findById(id);
		return entity;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> update(UserEntity entity){
		System.out.println("进入controller方法...");
			//影响的记录的行数
		Map<String,Object> map = new HashMap<String, Object>();
		
		try{
			service.update(entity);
			map.put("code", "success");
			map.put("msg", "修改成功...");
		}catch(Exception e){
			map.put("code", "failure");
			map.put("msg", "修改失败...");
		}
		System.out.println(map);
		
		System.out.println(new Gson().toJson(map));
		return map;
	}
	
	/**

	 * ids  11,12,13

	 * @param ids

	 * @return

	 */
	@RequestMapping(value="/deleteByIds")
	@ResponseBody
	public Map<String,Object> deleteByIds(@RequestBody List<Integer> ids){
		
		Map<String,Object> reuslt = new HashMap<String,Object>();
		
		service.deleteByIds(ids,reuslt);
		return reuslt;
	}




}
